import { createRouter, createWebHistory } from 'vue-router';
import CampusBinhai from '../components/CampusBinhai.vue';
import CampusChashan from '../components/CampusChashan.vue';
import CampusYueqing from '../components/CampusYueqing.vue';
import UserLogin from '../components/UserLogin.vue'; // 修改为 UserLogin.vue
import StudentSystem from '../components/StudentSystem.vue';

const routes = [
  { path: '/', component: CampusBinhai }, // 设置首页为 CampusBinhai.vue
  { path: '/chashan', component: CampusChashan },
  { path: '/yueqing', component: CampusYueqing },
  { path: '/login', component: UserLogin }, // 修改为 UserLogin.vue
  { path: '/student-system', component: StudentSystem }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 添加路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/student-system') { // 如果跳转到学生管理系统页面
    if (!localStorage.getItem('loggedIn')) { // 检查是否登录
      next('/login'); // 如果未登录，则跳转到登录页面
    } else {
      next(); // 如果已登录，则允许跳转
    }
  } else {
    next(); // 其他页面直接放行
  }
});

export default router;
